package com.sl.biz.modular.restapi.controller;


import cn.hutool.core.util.ReflectUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sl.biz.modular.ai.controller.domain.request.AiGenerateEntityRequest;
import com.sl.biz.modular.restapi.controller.request.ApiConfigQueryByIdRequest;
import com.sl.biz.modular.restapi.controller.request.ApiConfigUpdateByIdRequest;
import com.sl.biz.modular.restapi.controller.response.ApiConfigQueryByIdVO;
import com.sl.biz.modular.restapi.controller.response.ApiVersionConfigQueryByApiIdVO;
import com.sl.biz.modular.restapi.dao.entity.ApiConfigDO;
import com.sl.biz.modular.restapi.dao.entity.ApiVersionConfigDO;
import com.sl.biz.modular.restapi.service.ApiConfigBizService;
import com.sl.common.pojo.CommonResult;
import com.sl.core.domain.vo.ProcessFrontDefineBaseVO;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/config")
@RequiredArgsConstructor
public class ApiConfigController {


    private final ApiConfigBizService apiConfigBizService;

    @SneakyThrows
    @PostMapping("/save")
    public Comparable<String> save(@RequestBody ApiConfigDO apiConfigDO) {
        return apiConfigBizService.save(apiConfigDO);
    }


    @SneakyThrows
    @PostMapping("/update_by_id")
    public Boolean updateById(@RequestBody ApiConfigUpdateByIdRequest apiConfigUpdateByIdRequest) {
        return apiConfigBizService.updateById(apiConfigUpdateByIdRequest);
    }
    @SneakyThrows
    @PostMapping("/remove_by_id")
    public Boolean removeById(@RequestBody ApiConfigDO apiConfigDO) {
        return apiConfigBizService.removeById(apiConfigDO);
    }

    @SneakyThrows
    @PostMapping("/query_by_id")
    public ApiConfigQueryByIdVO queryById(@RequestBody ApiConfigQueryByIdRequest apiConfigUpdateByIdRequest) {
        return apiConfigBizService.queryById(apiConfigUpdateByIdRequest);
    }

    @SneakyThrows
    @PostMapping("/version/query_by_api_id")
    public List<ApiVersionConfigQueryByApiIdVO> queryVersionByApiId(@RequestBody ApiConfigQueryByIdRequest apiConfigUpdateByIdRequest) {
        return apiConfigBizService.queryVersionByApiId(apiConfigUpdateByIdRequest);
    }

    @SneakyThrows
    @PostMapping("/version/set_latest_flag")
    public Boolean versionSetLatestFlag(@RequestBody ApiVersionConfigDO apiVersionConfigDO) {
        return apiConfigBizService.versionSetLatestFlag(apiVersionConfigDO);
    }


    @SneakyThrows
    @PostMapping("/get_default_response_wrapper")
    public List<Map<String, Object>> getDefaultResponseWrapper() {

        Field[] fields = ReflectUtil.getFields(CommonResult.class);
        List<Map<String, Object>> resultList = Lists.newArrayList();
        for (Field field : fields) {
            if (field.getName().equals("serialVersionUID")) {
                continue;
            }
            Map<String, Object> result = Maps.newHashMap();
            result.put("code", field.getName());
            result.put("name", field.getName());
            result.put("type", field.getType().getSimpleName());

            resultList.add(result);
        }
        return resultList;
    }


    @SneakyThrows
    @PostMapping("/list")
    public List<ApiConfigDO> list(@RequestBody ApiConfigDO apiConfigDO) {
        return apiConfigBizService.list(apiConfigDO);
    }

    @SneakyThrows
    @PostMapping("/set_api_status_code")
    public Boolean setApiStatusCode(@RequestBody ApiConfigDO apiConfigDO) {
        return apiConfigBizService.setApiStatusCode(apiConfigDO);
    }
    @PostMapping("/ai_generate_process_file")
    public ApiVersionConfigQueryByApiIdVO aiGenerateProcessFile(@RequestBody AiGenerateEntityRequest aiGenerateEntityRequest){
        return apiConfigBizService.aiGenerateProcessFile(aiGenerateEntityRequest);
    }
}
